<?
class RegisterAction{
	
	public $smarty;
	public $name;
    public $openid;
    public $phone;
    public $js_sdk;
    private $model;

    public $is_reg;

	//构造方法，初始化 
	public function __construct(&$_smarty,&$_js_sdk,&$_model) {
		$this->smarty = $_smarty;
        $this->js_sdk = $_js_sdk;
        $this->model = $_model;
	}

    /**
     * 显示页面
     */
	public function action(){
        if(!empty($this->name)){
            $this->smarty->assign('name',$this->name);
        }
        if(!empty($this->openid)){
            $this->smarty->assign('openid',$this->openid);
            $this->smarty->assign('like_count',$this->getLikeCount($this->openid));
        }
        $this->smarty->assign('is_reg',$this->is_reg);
        // 用户列表
        $this->smarty->assign('userList', $this->model->getUserOrderByLikeCount());
        if(!empty($this->js_sdk)){
            $signPackage = $this->js_sdk->GetSignPackage();
            $this->smarty->assign('appId',$signPackage["appId"]);
            $this->smarty->assign('timestamp',$signPackage["timestamp"]);
            $this->smarty->assign('nonceStr',$signPackage["nonceStr"]);
            $this->smarty->assign('signature',$signPackage["signature"]);
        }
		$this->smarty->display('templates/register.tpl');
	}

    /**
     * 用户注册的方法
     * @param $name
     * @param $phone
     * @param $openid
     */
    public function register(&$name,&$phone,&$openid){
        if(!empty($name) && !empty($phone) && !empty($openid)){
            // TODO 数据库处理 验证
            $this->model->phone = $phone;
            $this->model->openid = $openid;
            if($this->model->checkUserToPhoneOrOpenid()){
                echo 'no_该微信用户已提交资料或手机号码已存在';
            }else{
                $this->model->name = $name;
                if($this->model->addUser()){
                    echo 'ok_提交成功_'.$name;
                }else{
                    echo 'no_提交失败';
                }
            }
        }else{
            echo 'no_数据不可为空';
        }
        exit;
    }

    /**
     * 存在openid
     * @param $openid
     * @return mixed
     */
    public function existsOpenid(&$openid){
        $this->model->openid = $openid;
        return $this->model->checkUserToPhoneOrOpenid();
    }

    /**
     * 存在openid
     * @param $openid
     * @return mixed
     */
    public function getLikeCount(&$openid){
        return $this->model->getLikeCountToOpenid($openid);
    }




    /**
     * 分享
     */
    public  function  share(){

    }
	
}